import {
  getPackage_items,
  savePackage,
  delPackage,
  saveCard,
  getCardInfo,
  cardDel
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    selectItemsArr: [], //已选择的检测项目
    packageName: '', //套餐名
    packagePrice: '', //套餐价格
    packageEndTime: '', //套餐有效期
    packageRemark: '', //套餐说明
    id: "", //套餐id
    isEdit: false, //是否是修改
    dataindex: 0, //当前选中选项卡  0套餐 1充值卡

    cardPrice: "", //充值金额
    cardGivePrice: "", //赠送金额
    cardRemark: "", //赠送金额
    cardEndTime: "", //充值卡有效期
    cardId:"",//会员卡id
  },
  handleZanStepperChange(e) {
    const componentId = e.target.dataset.componentId;
    const stepper = e.detail;
    this.setData({
      [`selectItemsArr[${componentId}].count`]: stepper,
    });
  },
  //跳转至添加项目
  goAddProItem: function() {
    wx.navigateTo({
      url: '../addproitem/addproitem?items=' + JSON.stringify(this.data.items),
    })
  },
  //切换选项卡
  toShowhidden: function(e) {
    if (e.currentTarget.dataset.index == this.data.dataindex) {

    } else {
      this.setData({
        dataindex: e.currentTarget.dataset.index
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    self.setData({
      dataindex: options.tagindex ? options.tagindex : 0,
      isEdit: options.isEdit ? options.isEdit :false 
    })
    if (this.data.dataindex == 0) {
      //套餐
      getPackage_items((r) => {
        if (r.code == 0) {
          //如果是点击项目进来。自动填上信息展示项目
          if (options.item) {
            wx.setNavigationBarTitle({
              title: "编辑套餐"
            })
            let itemInfo = JSON.parse(options.item)
            r.data.map((o, i) => {
              o.children.map((oo, ii) => {
                itemInfo.children.map((ooo, iii) => {
                  if (oo.id == ooo.id) {
                    oo.isSelect = true;
                    self.data.selectItemsArr.push(ooo);
                  }
                })
              })
            })
            self.setData({
              items: r.data,
              selectItemsArr: self.data.selectItemsArr,
              packageName: itemInfo.title, //套餐名
              packagePrice: itemInfo.price, //套餐价格
              packageEndTime: itemInfo.end, //套餐有效期
              packageRemark: itemInfo.description, //套餐说明
              id: itemInfo.id,
              isEdit: options.isEdit
            })

          } else {
            self.setData({
              items: r.data
            })
          }
        } else {
          errorMsg(r.message);
        }
      })
    } else if (this.data.dataindex == 1) {
      //充值卡

      //如果是点击项目进来。自动填上信息展示项目
      if (options.item) {
        wx.setNavigationBarTitle({
          title: "编辑充值卡"
        })
        let itemInfo = JSON.parse(options.item)

        self.setData({
          cardPrice: itemInfo.charge, //充值金额,
          cardGivePrice: itemInfo.give, //赠送金额
          cardRemark: itemInfo.description, //充值卡说明
          cardEndTime: itemInfo.end, //充值卡有效期
          cardId: itemInfo.id,
          isEdit: options.isEdit
        })

      }
    }


  },
  onShow: function() {
    let self = this;
    if (self.data.dataindex == 0) {
      if (app.globalData.packageItem) {
        self.data.selectItemsArr = [];
        app.globalData.packageItem.map((o, i) => {
          o.children.map((oo, ii) => {
            if (oo.isSelect) {
              oo.count = 1;
              self.data.selectItemsArr.push(oo)
            }
          })
        })
        self.setData({
          items: app.globalData.packageItem,
          selectItemsArr: this.data.selectItemsArr
        })
      }
    }

  },

  //获取套餐名
  packageNameChange: function(e) {
    this.setData({
      packageName: e.detail.detail.value
    })
  },
  //获取价格
  packagePriceChange: function(e) {
    this.setData({
      packagePrice: e.detail.detail.value
    })
  },
  //获取说明
  packageRemarkChange: function(e) {
    this.setData({
      packageRemark: e.detail.detail.value
    })
  },
  //获取有效期
  bindDateChange: function(e) {
    this.setData({
      packageEndTime: e.detail.value
    })
  },

  //获取充值卡金额
  cardPriceChange: function(e) {
    this.setData({
      cardPrice: e.detail.detail.value
    })
  },
  //获取充值卡赠送金额
  cardGivePriceChange: function(e) {
    this.setData({
      cardGivePrice: e.detail.detail.value
    })
  },
  //获取充值卡说明
  cardRemarkChange: function(e) {
    this.setData({
      cardRemark: e.detail.detail.value
    })
  },
  //获取充值卡有效期
  cardEndTimeChange: function(e) {
    this.setData({
      cardEndTime: e.detail.value
    })
  },
  //保存套餐
  onSave: function() {

    let {
      selectItemsArr,
      packageName,
      packagePrice,
      packageEndTime,
      packageRemark,
      id
    } = this.data
    savePackage(packageName, packagePrice, packageEndTime, id, selectItemsArr, packageRemark, (r) => {
      if (r.code == 0) {
        app.globalData.packageItem = null; //清空套餐中选择的项目 
        wx.navigateBack();
      } else {
        errorMsg(r.message)
      }

    })
  },
  //保存充值卡
  onSaveCard: function() {
    let {
      cardPrice,
      cardGivePrice,
      cardRemark,
      cardEndTime,
      cardId
    } = this.data
    saveCard(cardId,cardPrice, cardGivePrice, cardRemark, cardEndTime, (r) => {
      if (r.code == 0) {
        wx.navigateBack();
      } else {
        errorMsg(r.message)
      }
    })
  },
  onUnload: function() {
    app.globalData.packageItem = null; //清空套餐中选择的项目
  },
  //删除套餐
  onDel: function() {
    let self = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除这个套餐吗？',
      success: function(res) {
        if (res.confirm) {
          delPackage(self.data.id, (r) => {
            if (r.code == 0) { 
              wx.navigateBack();
            } else {
              errorMsg(r.message)
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })
  },
  //删除充值卡
  onDelCard: function() {

    let self = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除这个充值卡吗？',
      success: function(res) {
        if (res.confirm) {
          cardDel(self.data.cardId, (r) => {
            if (r.code == 0) { 
              wx.navigateBack();
            } else {
              errorMsg(r.message)
            }
          })
        } else if (res.cancel) {
          //点击取消
        }
      }
    })
  }




})